﻿@model Tuple<List<deviceManage.Models.DeviceType>,int?>
@{
    ViewData["Title"] = "设备台账";
}

 

        <div class="layui-fluid">
            <div class="layui-card">
                <div class="layui-form layui-card-header layuiadmin-card-header-auto search" lay-filter="app-content-list">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">设备名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" placeholder="请输入" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">设备编号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="deviceId" placeholder="请输入" autocomplete="off" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">设备类型</label>
                            <div class="layui-input-inline">
                                <select name="deviceTypeId">
                                    <option></option>
                                    @foreach (var type in Model.Item1)
                                    {
                                    <option value="@type.Id">@type.Name</option>
                                    }
                                </select>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">维修日期</label>
                            <div class="layui-input-inline">
                                <input type="text" name="start" class="layui-input date" id="test-laydate-start" placeholder="开始日期" lay-key="122">
                            </div>
                            <div class="layui-form-mid">
                                -
                            </div>
                            <div class="layui-input-inline">
                                <input type="text" name="end" class="layui-input date" id="test-laydate-end" placeholder="结束日期" lay-key="123">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button class="layui-btn layuiadmin-btn-list" lay-submit="" lay-filter="search">
                                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>查询
                            </button>
                            @if (1 != Model.Item2) {
                                <button onclick="goAddLog()" class="layui-btn">添加维修记录</button>
                            }
                        </div>
                    </div>
                </div>

                <div class="layui-card-body">                    
                    <table id="LAY-app-content-list" lay-filter="LAY-app-content-list"></table>
                </div>
            </div>
        </div>
 

<script type="text/javascript" src="~/js/deviceState.js"></script>
<script>
   

    var laydate = layui.laydate;
    var insStart = laydate.render({
        elem: '#test-laydate-start'
        , min: '1980-1-1'
        , done: function (value, date) {
            //更新结束日期的最小日期
            insEnd.config.min = lay.extend({}, date, {
                month: date.month - 1
            });

            //自动弹出结束日期的选择器
            insEnd.config.elem[0].focus();
        }
    });

    //结束日期
    var insEnd = laydate.render({
        elem: '#test-laydate-end'
        , min: '1980-1-1'
        , done: function (value, date) {
            //更新开始日期的最大日期
            insStart.config.max = lay.extend({}, date, {
                month: date.month - 1
            });
        }
    });

    form.render(null, 'app-content-list');

    //监听搜索
    form.on('submit(search)', function (data) {
        var field = data.field;
        //执行重载
        table.reload('LAY-app-content-list', {
            where: field
        });
    });
     

        //加载table
    var tableObj= table.render({
            elem: '#LAY-app-content-list'
            , url: '/deviceInfo/RepairLogAll/'  
            ,method: 'post'
            , height:"full-150"
            , page: true
            , cols: [[
                 
                 { field: 'name', title: '设备名称', minWidth: 100, templet: '<div><a class="link" onclick="showDetail(\'{{d.device.id}}\')"  href="#">{{d.device.name}}</a></div>'  } 
                , { field: 'name', title: '设备编号', width: 120, templet: '<div>{{ d.device.deviceId}}</div>' }
                , { field: 'name', title: '送修部门', width: 120, templet: '<div>{{ d.depart}}</div>' }
                , { field: 'name', title: '维修原因', width: 200, templet: '<div>{{ getString(d.remark)}}</div>' }
                , { field: 'name', title: '开始时间', width: 120, templet: '<div>{{ getDate(d.beginDate)}}</div>' }  
                , { field: 'name', title: '结束时间', width: 120, templet: '<div>{{ getDate(d.endDate)}}</div>' } 
                , { field: 'time', title: '解决方案', width: 100, templet: '<div>{{ getString(d.solution)}}</div>' } 
                , {
                    field: 'opr', title: '操作', width: 100, fixed:'right',
                templet:
                '<div class="layui-table-cell laytable-cell-11-0-8">'                
                    + ' {{ getButtons(d.id,d.status) }} '                    
                + '</div>'
            }
            ]]
            , skin: 'line'
        });

    function getDate(d) {
        if (d != null) {
            return d.substring(0, 10);
        } else {
            return "";
        }
    }

    //根据状态获取不同的按钮
    function getButtons(id, state) {            
        return ' <a class="layui-btn layui-btn-danger layui-btn-xs" onclick="deleteItem(\'' + id + '\')"><i class="layui-icon layui-icon-delete"></i>删除</a> ';
    }

    //删除记录
    function deleteItem(id) {
        layer.confirm("确定要删除记录吗？", function (index) {
            layer.close(index);
            $.post("/deviceInfo/DeleteRepairLog?id=" + id, function (res) {
                layer.msg(res.msg);
                if (res.code == 1) {
                    tableObj.reload();
                }
            });
        });
    }
    

    //维修页面
    function goAddLog() {
        $.get("/deviceinfo/GoAddRepairLog", function (str) {
            var index = layer.open({
                type: 1,
                area:["600px","500px"],
                title: '维修',
                content: str //注意，如果str是object，那么需要字符拼接。
            });

            form.render("", 'repairForm');
            var repairStart = laydate.render({
                'elem': '#beginDate',
                 done: function (value, date) {
                    //更新结束日期的最小日期
                     repairEnd.config.min = lay.extend({}, date, {
                        month: date.month - 1
                    });

                    //自动弹出结束日期的选择器
                     repairEnd.config.elem[0].focus();
                }
            });
            var repairEnd = laydate.render({
                'elem': '#endDate', done: function (value, date) {
                    //更新开始日期的最大日期
                    repairStart.config.max = lay.extend({}, date, {
                        month: date.month - 1
                    });
                } });

            //监听提交
            form.on('submit(saveRepair)', function (data) {
                var field = data.field; //获取提交的字段 
                //提交 Ajax 成功后，关闭当前弹层并重载表格
                $.post("/deviceInfo/AddRepairLog", field, function (d) {
                    layer.msg(d.msg);
                    if (d.code == 1) {
                        layer.close(index); tableObj.reload();
                    }
                });

            });
        });
    } 

    function showDetail(id) {
        layer.open({
            mask: 0,
            type: 2,
            content: '/deviceInfo/Detail?id=' + id,
            area: ["1000px", "550px"]
        });
    }
    
</script>

